package com.myway.components.payment.yeepay.interFace;

import java.util.Map;

import com.myway.components.payment.yeepay.util.DigestUtil;


/**
 * 类名称：DigestUtilInterFace <br/>
 * 类描述：添加加密字符串 <br/>
 * 创建时间：2015年11月23日 下午9:39:27 <br/>
 * @author 张涛
 * @version V1.0
 */
public class DigestUtilInterFace {
	
	/**
	 * 名称：addHmac <br/>
	 * 描述：添加加密参数 <br/>
	 * @param HmacOrder
	 * @param map
	 * @param keyValue
	 * @return
	 */
	public static Map addHmac(String[] HmacOrder, Map map, String keyValue){
		map = formatMap(HmacOrder, map);
		String sbold = DigestUtil.getHmacSBOld(HmacOrder, map);
		
		String hmac = "";
		String str = sbold; 
		hmac = DigestUtil.hmacSign(sbold, keyValue);
		map.put("hmac", hmac);
//		logUtil.log("[KeyValue]" + keyValue + ",[sbold]" + sbold + ",[hmac]" + hmac);
		return map;
	}
	
	/**
	 * 名称：formatMap <br/>
	 * 描述：格式化map <br/>
	 * @param HmacOrder
	 * @param map
	 * @return
	 */
	public static Map formatMap(String[] HmacOrder, Map map){
		String key = "";
		String value = "";
		for(int i = 0; i < HmacOrder.length; i++){
			key = HmacOrder[i];
			value = (String)map.get(key);
			if(value == null){
				map.put(key, "");
			}
		}
		return map;
	}
	
	/**
	 * 名称：checkHmac <br/>
	 * 描述：检查签名 <br/>
	 * @param HmacOrder
	 * @param map
	 * @param keyValue
	 * @return
	 */
	public static boolean checkHmac(String[] HmacOrder,Map map, String keyValue){
		boolean returnBoolean = false;
		Object hmacObj = map.get("hmac");
		String hmac = (hmacObj == null) ? "" : (String)hmacObj ;
		String sbold = DigestUtil.getHmacSBOld(HmacOrder, map);
		String newHmac = "";
		newHmac = DigestUtil.hmacSign(sbold, keyValue);
		if(hmac.equals(newHmac)){
			returnBoolean = true;
		}
//		logUtil.log("[hmac]" + hmac + ",[keyvalue]" + keyValue + ",[sbold]" + sbold + ",[newHmac]" + newHmac);
		return returnBoolean; 
	}

}
